package common.html;

import java.util.Map;
import java.util.TreeMap;
import common.html.Tag.Text;

public class TagFactory
{

    public static DefaultTag createTag(String tagName, Map<String, String> attr)
    {
        DefaultTag dt = new DefaultTag(tagName);
        dt.putAllAttr(attr);
        return dt;
    }

    public static DefaultTag createTag(String tagName)
    {
        DefaultTag dt = new DefaultTag(tagName);
        return dt;
    }

    public static DefaultTag createTag(String tagName, String[][] attr)
    {
        DefaultTag dt = new DefaultTag(tagName);
        TreeMap<String, String> tm = new TreeMap<String, String>();
        if (null != attr)
        {
            for (String[] keyValue : attr)
            {
                if (keyValue.length > 1)
                {
                    tm.put(keyValue[0], keyValue[1]);
                }
                else if (keyValue.length == 1)
                {
                    tm.put(keyValue[0], keyValue[0]);
                }
            }
        }
        dt.putAllAttr(tm);
        return dt;
    }

    public static DefaultTag createTag(String tagName, String content)
    {
        return createTag(tagName, content, null);
    }

    public static DefaultTag createTag(String tagName, String content, String[][] attr)
    {
        DefaultTag df = createTag(tagName, attr);
        df.after(createText(content));
        return df;
    }

    public static Text createText(String format, String... args)
    {
        return new Text(format, args);
    }

    public static LinkTag createLink(String href)
    {
        return new LinkTag(href);
    }

    public static LinkTag createLink(String href, String content)
    {
        return createLink(href).apped(content);
    }

    public static ScriptTag createScript()
    {
        return new ScriptTag("");
    }
    public static ScriptTag createScript(String src)
    {
        return new ScriptTag(src);
    }

    public static ScriptTag createScript(String src, String content)
    {
        ScriptTag s = createScript(src);
        s.apped(content);
        return s;
    }
}
